<?php 
defined('RUN_KATANA') or die('Restricted access');
class AutoNav
{
	public $Sitemap;
	public $StartId;
	public $StartLevelId;
	public $EndLevel;
	public $Params;
	
	public function AutoNav($smap, $startId=0, $startLevel=0, $params=array()) 
	{ 
		self::setSitemap($smap); 
		self::setStartId($startId); 
		self::setStartLevelId($startLevel);
		self::setParams($params);
	}
	
	/* Get Functions */
	public function getSitemap() { return $this->Sitemap; }
	public function getStartId() { return $this->StartId; }
	public function getStartLevelId() { return $this->StartLevelId; }
	public function getEndLevel() { return intval($this->EndLevel); }
	public function getParams() { return $this->Params; }
	
	/* Set Functions */
	public function setSitemap($val) { $this->Sitemap=$val; }
	public function setStartId($val) { $this->StartId=$val; }
	public function setStartLevelId($val) { $this->StartLevelId=$val; }
	public function setEndLevel($val) { $this->EndLevel=intval($val); }
	public function setParams($val) { $this->Params=$val; }
	
	
	public function getHtml($embed='')
	{
		global $sitemap;
		if($sitemap)
		{
			$lev = 0;
			$startid = $this->StartId;
			
			$output .= '<div ';
			if(is_array($this->Params['div']))
			{
				foreach(array_keys($this->Params['div']) as $param)
				{ $output .= $param.'="'.$this->Params['div'][$param].'"'; }
			}
			$output .= '>';
			if($embed['div'] != '')
			{ $output .= $embed['div']; }
			$output .= '<nav ';
			if(is_array($this->Params['nav']))
			{
				foreach(array_keys($this->Params['nav']) as $param)
				{ $output .= $param.'="'.$this->Params['nav'][$param].'"'; }
			}
			$output .= '>';
			if($embed['nav'] != '')
			{ $output .= $embed['nav']; }
			$output .= '<ul ';
			if(is_array($this->Params['ul']))
			{
				foreach(array_keys($this->Params['ul']) as $param)
				{ $output .= $param.'="'.$this->Params['ul'][$param].'"'; }
			}
			$output .= '>';
			if($embed['ul'] != '')
			{ $output .= $embed['ul']; }
			if($sitemap)
			{
				if($startid == 0)
				{
					$startid = $sitemap->SitemapItems->getId();
					$output .= '<li>';
						
						
						if($embed['frontpage'] != '')
						{ 
							$output .= '<a data-toggle="dropdown" class="dropdown-toggle" href="/">'.$sitemap->SitemapItems->getTitle().'&nbsp;&nbsp;<b class="caret"></b></a>';
							$output .= $embed['frontpage']; 
						}
						else
						{ $output .= self::getMakeLink($sitemap->SitemapItems, 'level_'.$lev.' first'); }
						
					$output .= '</li>'; 
					
				}
				if($embed['li'] != '')
				{ $output .= $embed['li']; }
				if($sitemap->SubItems[$startid])
				{
					$total = count($sitemap->SubItems[$startid]);
					$current = 1;
					
					foreach($sitemap->SubItems[$startid] as $subitem)
					{
						$linkHTML = '';
						$linkclass = 'level_'.$lev;
						if(($subitem->SubItems) && (self::getEndLevel() > 1))
						{ 
							$linkclass .= ' sub dropdown-toggle'; 
							$linkparams['data-toggle'] = "dropdown";
						}
						if($current == $total)
						{ $linkclass .= ' last'; }
						
						$class = '';
						if($subitem->getId() == $sitemap->getCurrentPageId())
						{ $class .= ' active'; }
						if(($subitem->SubItems) && (self::getEndLevel() > 1))
						{ $class .= ' dropdown'; }
						if(($subitem->LinkUrl != '') && ($_GET['m'] > 0))
						{
							if(stripos($subitem->LinkUrl,'/m/'.$_GET['m'].'/') > -1)
							{ $class .= ' active'; }
						}
						if(($subitem->getLinkType() == 2) && ($_GET['c'] == $subitem->getLinkId()))
						{ $class .= ' active'; }
						
						$linkHTML = self::getMakeLink($subitem, $linkclass, $linkparams);
						
						if($linkHTML != '')
						{
							$output .= '<li class="'.$class.'">';
							$output .= $linkHTML;
							if(($subitem->SubItems) && (self::getEndLevel() > 1))
							{
								$nextlev = $lev+1;
								$output .= '<ul class="submenu level_'.$nextlev.' dropdown-menu">';
									$output .= self::getSubitemsHtml($subitem->SubItems, $nextlev);
								$output .= '</ul>';
							}
							
							$output .= '</li>';
						}
						$current++;
					}
				}
			}
			$output .= '</ul>';
			$output .= '</nav>';
			$output .= '</div>';
			$output = str_replace('<li></li>', '', $output);
			return $output;
		}
	}
	
	private function getSubitemsHtml($subitems, $lev=0)
	{
		$output = '';
		if($subitems)
		{
			$total = count($subitems);
			$current = 1;
				
			foreach($subitems as $subitem)
			{
				$linkclass = 'level_'.$lev;
				if($subitem->SubItems)
				{ $linkclass .= ' sub'; }
				if($current == 1)
				{ $linkclass .= ' first'; }
				if($current == $total)
				{ $linkclass .= ' last'; }
				$output .= '<li>';
				//print_r($subitem);
				//if(($subitem->getVisible() > 0) && ($subitem->getActive() > 0))
				//{ $output .= $subitem->getLinkHTML($linkclass); }
				if(($subitem->getVisible()) && ($subitem->getActive()))
				{ $output .= self::getMakeLink($subitem, $linkclass); }
				if($subitem->SubItems)
				{
					$nextlev = $lev+1;
					$output .= '<ul class="submenu level_'.$nextlev.'">';
						$output .= self::getSubitemsHtml($subitem->SubItems, $nextlev);
					$output .= '</ul>';
				}
				$output .= '</li>';
				$current++;
			}
		}
		return $output;
	}
	
	private function getMakeLink($item, $class='', $params = array())
	{
		if(($item->getVisible()) && ($item->getActive()))
		{
			global $sitemap;
			if($item->getId() == $sitemap->getCurrentPageId())
			{ $class .= ' active'; }
			if(($item->LinkUrl != '') && ($_GET['m'] > 0))
			{
				if(stripos($item->LinkUrl,'/m/'.$_GET['m'].'/') > -1)
				{ $class .= ' active'; }
			}
			if(($item->getLinkType() == 2) && ($_GET['c'] == $item->getLinkId()))
			{ $class .= ' active'; }
			
			$output = '<a class="'.$class.' type_'.$item->LinkType.'" ';
			if(count($params) > 0)
			{
				foreach(array_keys($params) as $param)
				{ $output .= $param.'="'.$params[$param].'" '; }
			}
			if($item->LinkTarget == 1)
			{ $output .= ' target="_blank" '; }
			$output .= ' href="'.$item->getLink().'" ';
			$output .= '>'.$item->getTitle().'</a>';
		}
		return $output;
	}
	
	public function getXML()
	{
		global $sitemap;
		if($sitemap)
		{
			$lev = 0;
			$startid = $this->StartId;
			if($sitemap->SubItems[$startid])
			{
				$total = count($sitemap->SubItems[$startid]);
				$current = 1;
				
				$priority = '1.00';
				$frequency = 'weekly';
				
				foreach($sitemap->SubItems[$startid] as $subitem)
				{
					if($subitem->getIsFrontpage() > 0)
					{ 
						$priority = '1.00';
						$frequency = 'daily';
					}
					if($subitem->LinkType == 2)
					{
						$priority = '0.20';
						$frequency = 'never';
					}
					if($subitem->LinkType == 0)
					{ 
						$priority = '0.50';
						$frequency = 'weekly';
					}
					
					$url = self::getMakeLinkXML($subitem);
					if(($url != '') && ($subitem->getId() > 1))
					{
						$output .= 	'<url>';
						$output .= 		'<loc>'.$url.'</loc>';
						if($subitem->LinkType == 2)
						{ 
							$output .= 		'<changefreq>'.$frequency.'</changefreq>'; 
							$output .= 		'<priority>'.$priority.'</priority>';
						}
						if($subitem->LinkType == 0)
						{ 
							$output .= 		'<changefreq>'.$frequency.'</changefreq>'; 
							$output .= 		'<priority>'.$priority.'</priority>';
						}
						$output .= 	'</url>';
					}
					if($subitem->SubItems)
					{
						$nextlev = $lev+1;
						$output .= self::getSubitemsXML($subitem->SubItems, $nextlev);
					}
					
					$current++;
				}
			}
			return $output;
		}
	}
	
	private function getSubitemsXML($subitems, $lev=0)
	{
		$output = '';
		if($subitems)
		{
			$total = count($subitems);
			$current = 1;
			$priority = 5-$lev;
			if($priority < 0)
			{ $priority = 1; }
			foreach($subitems as $subitem)
			{
				$url = self::getMakeLinkXML($subitem);
				if($url != '')
				{
					$output .= 	'<url>';
					$output .= 		'<loc>'.$url.'</loc>';
					if($subitem->LinkType == 2)
					{ 
						$output .= 		'<changefreq>never</changefreq>'; 
						$output .= 		'<priority>0.'.$priority.'0</priority>';
					}
					if($subitem->LinkType == 0)
					{ 
						$output .= 		'<changefreq>weekly</changefreq>'; 
						$output .= 		'<priority>0.'.$priority.'0</priority>';
					}
					$output .= 	'</url>';
				}
				if($subitem->SubItems)
				{
					$nextlev = $lev+1;
					$output .= self::getSubitemsXML($subitem->SubItems, $nextlev);
				}
				$current++;
			}
		}
		return $output;
	}
	
	private function getMakeLinkXML($item)
	{
		if($item->getActive() > 0)
		{
			if($item)
			{
				if($item->LinkType == 0)
				{
					if($item->getIsFrontpage() > 0)
					{ $output .= SITE_URL; }
					else
					{
						if(URL_REWRITE == true)
						{ $output .= SITE_URL.'m/'.$item->getId().'/'; }
						else
						{ $output .= SITE_URL.'?m='.$item->getId(); }
					}
				}
				if($item->LinkType == 2)
				{
					if(URL_REWRITE == true)
					{ $output .= SITE_URL.'c/'.$item->getLinkId().'/'; }
					else
					{ $output .= SITE_URL.'?c='.$item->getLinkId(); }
				}
			}
			return $output;
		}
	}
}
?>